/* ========================================================================== */
/*   SoundController.cpp    	                                              */
/*   (c) 2012 Team 7 Hogeschool Utrecht ICT TI                                */
/*		Lars Stolwijk, Gerben Boot, Guido van Hienen, Marcel Boelen			  */
/*                                                                            */
/*   Receives sound	commands and translates these commands to proper		  */
/*		Hertz and Seconds for the Beeper							          */
/* ========================================================================== */
#include "SoundController.h"

void SoundController::RegisterHealth(Health * health){
	theHealth = health;
}

void SoundController::main()
{
	int i = 0;
	CurrentState = RUNNING;
	for(;;)
	{
		if(CurrentState == RUNNING)
		{
			//std::cout << "SC->RUNNING1/3\n";
			RTOS::event e = wait( );
			//std::cout << "SC->RUNNING2/3\n";
			if(PlayStartGameSoundFlag == e)
			{
				i = 0;
				CurrentState = PLAYINGSTARTSOUND;
			}
			else if(SCTTimer == e)
			{
				i = 0;
				CurrentState = PLAYINGHEALTHSOUND;
			}
			else if(PlayReadLogSoundFlag == e)
			{
				theBeeper->PlaySound(700, 100);
			}
			else if(PlayIntroduceGameSoundFlag == e)
			{
				theBeeper->PlaySound(500, 500);
			} 
			else if(PlayHitSoundFlag == e)
			{
				theBeeper->PlaySound(350, 350);
				SCTTimer.set(850 MS);
			} 
			else if(PlayEndGameSoundFlag == e)
			{
				theBeeper->PlaySound(800, 100);
			} 
			else if(PlayDeathSoundFlag == e)
			{
				theBeeper->PlaySound(500, 100);
				theBeeper->PlaySound(300, 200);
			} 
			else if(PlayActivateDeactivatePlayerSoundFlag == e)
			{
				int hz = 500;
				do{
					theBeeper->PlaySound(hz, 100); 
					hz += 500;
				}while(hz <= 2500);
			} 
			//std::cout << "SC->RUNNING3/3\n";
		}
		else if(CurrentState == PLAYINGSTARTSOUND)
		{	
			//std::cout << "SC->PLAYINGSTARTSOUND1/2\n";
			theBeeper->PlaySound(400, 300);
			i++;
			SCTTimer.set(1300 MS);

			RTOS::event e = wait(SCTTimer );
			if(e == SCTTimer)
			{
				if(i >= 2)
				{
					theBeeper->PlaySound(600, 500);
					CurrentState = RUNNING;
				}
			}
			//std::cout << "SC->PLAYINGSTARTSOUND2/2\n";
		}
		else if(CurrentState == PLAYINGHEALTHSOUND)
		{ 
			//std::cout << "SC->PLAYINGHEALTHSOUND1/2\n";
			SCTTimer.set(100 MS);
			RTOS::event e = wait(SCTTimer );
			if(e == SCTTimer)
			{
				signed char h = theHealth->ReadHealth();
				//std::cout << h << " <-- Health i: " << i << "\n";
				if(i >= (h/10))//(h <= 100 && i >= (h/10))
				{         	
					CurrentState = RUNNING;
				}
				else
				{
					theBeeper->PlaySound(1000, 35);
					i++;
				}
			}
			//std::cout << "SC->PLAYINGHEALTHSOUND2/2\n";
		} 
    }
}

void SoundController::PlayIntroduceGameSound()
{
	PlayIntroduceGameSoundFlag.set();
}

void SoundController::PlayStartGameSound()
{
	PlayStartGameSoundFlag.set();
}

void SoundController::PlayEndGameSound()
{
	PlayEndGameSoundFlag.set();
}

void SoundController::PlayDeathSound()
{
	PlayDeathSoundFlag.set();
}

void SoundController::PlayActivateDeactivatePlayerSound()
{
	PlayActivateDeactivatePlayerSoundFlag.set();
}

void SoundController::PlayHitSound()
{
	PlayHitSoundFlag.set();
}

void SoundController::PlayReadLogSound()
{
	PlayReadLogSoundFlag.set();
}
